from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from coupons.models import *
from orders.models import *


# 优惠券   CouponType
class Coupon_Serializer(ModelSerializer):
    coupon_type_name = serializers.SerializerMethodField(read_only=True)
    good_name = serializers.SerializerMethodField(read_only=True)

    def get_coupon_type_name(self, obj):
        return obj.coupon_type.type_name

    # good 字段可以为空 为防止报错 先判断good是否存在
    def get_good_name(self, obj):
        if obj.good:
            return obj.good.name
        return ''

    class Meta:
        model = Coupon
        fields = '__all__'
        read_only_fields = ['id', ]


# 优惠券类型
class CouponType_Serializer(ModelSerializer):

    class Meta:
        model = CouponType
        fields = '__all__'
        read_only_fields = ['id', ]










